package org.giovanolli.mumi.config;

import java.io.InputStream;
import java.util.Properties;

import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.NumberTool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;

import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;

/**
 * The Class TemplateConfig.
 */
@Configuration
public class TemplateConfig {

	/**
	 * Date.
	 * 
	 * @return the date tool
	 */
	@Bean
	public DateTool dateTool() {
		return new DateTool();
	}

	@Bean
	public CSSResolver defaultCssResolver() {
		CSSResolver cssResolver = new StyleAttrCSSResolver();
		InputStream csspathtest = Thread.currentThread().getContextClassLoader().getResourceAsStream("appTemplates/template.css");
		CssFile cssFile = XMLWorkerHelper.getCSS(csspathtest);
		cssResolver.addCss(cssFile);
		return cssResolver;
	}

	/**
	 * Number tool.
	 * 
	 * @return the number tool
	 */
	@Bean
	public NumberTool numberTool() {
		return new NumberTool();
	}

	/**
	 * Velocity engine.
	 * 
	 * @return the velocity engine factory bean
	 */
	@Bean
	public VelocityEngineFactoryBean velocityEngine() {
		VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
		Properties velocityProperties = new Properties();
		velocityProperties.setProperty("resource.loader", "class");
		velocityProperties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		velocityEngineFactoryBean.setVelocityProperties(velocityProperties);
		return velocityEngineFactoryBean;
	}
}
